import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JLabel;

public class Flight extends Thread {
	Boolean isarrived = false;
	String Alliance;
	String Inbound; // NAME
	String Outbound;
	int STA;
	int STD;
	String ACType;
	int PaxIn; // number
	int PaxOut;
	int day;
	int month;
	int year;
	int date;

	ArrayList<Passenger> paxList = new ArrayList<Passenger>();

	public Flight(String Alliance, String Inbound, String Outbound, int STA,
			int STD, String ACType, int PaxIn, int PaxOut, int day, int month,
			int year) {

		this.Alliance = Alliance;
		this.Inbound = Inbound;
		this.Outbound = Outbound;
		this.STA = STA;
		this.STD = STD;
		this.ACType = ACType;
		this.PaxIn = PaxIn;
		this.PaxOut = PaxOut;
		this.day = day;
		this.month = month;
		this.year = year;
		for (int i = 0; i < PaxIn; i++) {
			Passenger p = new Passenger(i + 1, this);
			paxList.add(p);
		}

	}

	public void run() {

		try {
			synchronized (this) {
				this.wait((this.STA / 100) * 60000 + (this.STA % 100) * 16);
				System.out.println(this.Inbound + " has arrived");

				Launch.schedulePanel.add(new JLabel(this.Inbound
						+ " has arrived"));
				this.isarrived = true;
				this.assignBaggage();
				Launch.schedulePanel.validate();
				this.firststage();
				Launch.schedulePanel.validate();

			}

		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		synchronized (this) {
			this.notify();

		}

	}

	public void firststage() {

		// find the passport unit with the minimum queue
		Passenger p;
		PassportUnit pu;
		int counter = this.paxList.size();
		for (int i = 0; i < counter; i++) {
			pu = getMin(Launch.PassportList);
			p = this.paxList.remove(0);
			pu.queue.add(p);

		}
		for (int i = 0; i < Launch.PassportList.size(); i++) {
			Launch.textArea1.get(i).validate();
			Launch.textArea1.get(i)
					.setText(
							"Queue length : "
									+ Launch.PassportList.get(i).queue.size()+"\n" );
			Launch.textArea1.get(i).validate();

			// System.out.println("passenger unit with id " +
			// Launch.PassportList.get(i).id
			// + " has queue of length " +
			// Launch.PassportList.get(i).queue.size());
		}
		passportProcess(Launch.PassportList);

	}

	public PassportUnit getMin(LinkedList<PassportUnit> list) {
		PassportUnit pumin;
		pumin = list.get(0);
		// System.out.println("a");
		for (int i = 0; i < list.size(); i++) {

			if (list.get(i).queue.size() <= pumin.queue.size()) {
				// System.out.println("a");
				pumin = list.get(i);
			}
		}
		// System.out.println(pumin.id);

		return pumin;

	}

	public void passportProcess(LinkedList<PassportUnit> list) {

		for (int i = 0; i < list.size(); i++) {
			if (!list.get(i).isAlive())
				list.get(i).start();

		}

	}

	public void assignBaggage() {

		
			BaggageCarousel b = null;
			for (int i = 0; i < Launch.CarouselList.size(); i++)
				if (!Launch.CarouselList.get(i).busy) {
					b = getMinBaggageFree(this);
					break;
				} else
					b = getMinBaggageBusy(this);
			for (int i = 0; i < Launch.CarouselList.size(); i++) {
				if (b.id == Launch.CarouselList.get(i).id) {
					Launch.CarouselList.get(i).flightName = this.Inbound;
					Launch.CarouselList.get(i).currentNumber = (int) Math
							.ceil(this.paxList.size() * 2.5);
					Launch.CarouselList.get(i).busy = true;
					if (!Launch.CarouselList.get(i).isAlive())
						Launch.CarouselList.get(i).start();

				
			}

		}
	}

	public BaggageCarousel getMinBaggageFree(Flight flight) {

		BaggageCarousel baggage = null;
		LinkedList<BaggageCarousel> temp = new LinkedList<BaggageCarousel>();
		for (int i = 0; i < Launch.CarouselList.size(); i++)
			if (!Launch.CarouselList.get(i).busy)
				temp.add(Launch.CarouselList.get(i));
		for (int i = 0; i < temp.size(); i++)
			if (temp.get(i).capacity < Math.ceil(flight.PaxIn * 2.5))
				temp.remove(i);

		if (temp.size() == 0) {
			System.out.println("Big Eroor");
		}
		baggage = temp.get(0);

		for (int i = 0; i < temp.size(); i++) {

			if (temp.get(i).capacity <= baggage.capacity)
				baggage = temp.get(i);

		}
		baggage.flightName = flight.Inbound;

		for (int i = 0; i < flight.paxList.size(); i++) {
			paxList.get(i).flight.Inbound = flight.Inbound;
		}

		Launch.textArea2.get(baggage.id - 1).setText(
				"Serves flight " + flight.Inbound);
		
		return baggage;
		// System.out.println("Baggage Number " + baggage.id + " serves flight "
		// + flight.Name);

	}

	public BaggageCarousel getMinBaggageBusy(Flight flight) {
		BaggageCarousel b = null;
		for (int i = 0; i < Launch.CarouselList.size(); i++) {
			if (Launch.CarouselList.get(i).capacity
					- Launch.CarouselList.get(i).currentNumber > Math
					.ceil(flight.PaxIn * 2.5)) {
				b = Launch.CarouselList.get(i);
				b.flightName=flight.Inbound;
				for (int j = 0; j < flight.paxList.size(); j++) {
					paxList.get(j).flight.Inbound = flight.Inbound;
				}

				break;
			}
		}

		Launch.textArea2.get(b.id - 1).setText(
				"Serves flight " + flight.Inbound);
		return b;

		

	}

}
